import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * Created by Shirly on 2016/8/9.
 */
public class CheckServlet extends HttpServlet {
    private static final long serialVersionUID = 50477937283143164L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String clientCheckcode = req.getParameter("validateCode");//接收客户端浏览器提交上来的验证码
        String serverCheckcode = (String) req.getSession().getAttribute("checkcode");//从服务器端的session中取出验证码
       resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        resp.setHeader("content-tyep","text-html;charset=utf-8");
        if (clientCheckcode.equals(serverCheckcode)) {//将客户端验证码和服务器端验证比较，如果相等，则表示验证通过
            System.out.println("验证码验证通过！");
            out.print("验证码验证通过！");
        }else {
            System.out.println("验证码验证失败！");
            out.print("验证码验证失败！");
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
